<?php

/**
 * Controller_quantification
 * 班级量化
 */

class Controller_quantification extends MyApp {


    /** 首页 **/
    public function actionIndex()
    {

    }

    /** 班级量化列表 **/
    public function actionList ()
    {	
		// 全部年级
		$grade_lists = $this->db->fetchAll("SELECT `grade_name`,`grade_id` FROM `tbl_grade`");
		// 全部班级
		$class_lists = $this->db->fetchAll("SELECT `class_name`,`class_id` FROM `tbl_class`");

		// 查询条件
		$grade_id		= $this->get->grade_id;
		$class_id		= $this->get->class_id;
		$from			= $this->get->from;
		$to				= $this->get->to;
	
		$where = " ";
	
		if(!empty($grade_id)){
			$where .= " AND a.grade_id = '{$grade_id}' ";
			$this->tpl->assign("grade_id", $grade_id);
		}

		if(!empty($class_id)){
			$where .= " AND a.class_id = '{$class_id}' ";
			$this->tpl->assign("class_id", $class_id);
		}

		if(!empty($from)){
			$where .= " AND a.create_time > '{$from}' ";
			$this->tpl->assign("from", $from);
		}
		
		if(!empty($to)){
			$where .= " AND a.create_time < '{$to}' ";
			$this->tpl->assign("to", $to);
		}
    	 // 分页     
        YOU::loadLib('page', "", '0');
        $tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_manage` AS a,`tbl_grade` AS b WHERE a.grade_id=b.grade_id {$where}");
        $count = $tmp['count'];
        $page_total = ceil($count/PAGE_LIMIT);
        // 当前页                  
        $p = intval($_GET['p']);
        if ($p == 0){
            $p  = 1;
        }
        if ($p > 1){	
            $start = PAGE_LIMIT * ($p-1);
        }else{
            $start = 0;
        }
		// 7=班级量化
        $manage_lists = $this->db->fetchAll("SELECT a.*,b.grade_name FROM `tbl_manage`AS a,`tbl_grade` AS b WHERE a.`manage_type`='7' AND a.grade_id=b.grade_id {$where} ORDER BY a.manage_id DESC LIMIT {$start}, ".PAGE_LIMIT."");
		
		foreach($manage_lists as $key=>$value){
			
			// 教师名称（提交者）
			$staff_data = $this->db->fetchRow("SELECT `staff_name` FROM `tbl_staff` WHERE staff_id={$value['staff_id']}");
			$manage_lists[$key]['staff_name'] = $staff_data['staff_name'];
			
			// 班级	
			$class_data = $this->db->fetchRow("SELECT `class_name` FROM `tbl_class` WHERE class_id={$value['class_id']}");
			$manage_lists[$key]['class_name'] = $class_data['class_name'];
			
		}		

        $page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
        $page->next_page   = '下一页>';
        $page->pre_page    = '<上一页';
        $page->style_path  = "static/page_style";
       
	    $this->tpl->assign("page", $page->show(4, "yahoo"));
		$this->tpl->assign("manage_lists", $manage_lists);
		$this->tpl->assign("class_lists", $class_lists);
		$this->tpl->assign("grade_lists", $grade_lists);
		
        $this->tpl->display('quantification/list.tpl.php');
    }
	/** 班级量化名称添加 **/
    public function actionNameAdd()
    {        
					
        if ($_POST){ 

			$quantification_name   		= $this->post->quantification_name;

            // 写入数据库
            $info = array( 
				
			"quantification_name"   => $quantification_name,
            "status"                => 1,
            "create_time"           => date("Y-m-d H:i:s"),
            "update_time"           => "0000-00-00 00:00:00",  

			 );

          $this->db->do_insert("tbl_quantification", $info);
          Helper_Client::location("添加成功", "index.php?a=quantification.nameadd");

         }
         $this->tpl->display("quantification/name.add.tpl.php");
    }
	/** 班级量化显示添加、显示 **/
    public function actionShowName()
    {   
		// 全部量化名称
		$quantification_lists = $this->db->fetchAll("SELECT * FROM `tbl_quantification`");
			
        if ($_POST){ 
			
            $quantification_id     	= $this->post->quantification_id; 
			// 全部班级名称
			$class_lists = $this->db->fetchAll("SELECT `class_name` FROM `tbl_class`");

			foreach($quantification_id as $key=>$value){
				// 获得对应量化名称
				$quantification_data = $this->db->fetchRow("SELECT * FROM `tbl_quantification` WHERE quantification_id={$value}");
				$quantification_new_lists[$key]['quantification_name']=$quantification_data['quantification_name'];
				
			}
			foreach($class_lists as $key=>$value){
				// 获得对应量化名称
				
				$class_lists[$key]['quantification_name']=$quantification_new_lists;
				
			}
			
            
			$this->tpl->assign("quantification_new_lists", $quantification_new_lists);
			$this->tpl->assign("class_lists", $class_lists);
			$this->tpl->display("quantification/list.tpl.php");
         }else{
		
		
		$this->tpl->assign("quantification_lists", $quantification_lists);
		$this->tpl->display("quantification/show.name.tpl.php");
		 }
		
    }
    /** 班级量化修改 **/
    public function actionUpdate()
    {   
		// 标题
        $manage_id   = (int)$this->get->manage_id;
        $manage_data = $this->db->fetchRow("SELECT * FROM `tbl_manage` WHERE `manage_id`='{$manage_id}'");
		// 年级	
		$grade_lists = $this->db->fetchAll("SELECT `grade_id`,`grade_name` FROM `tbl_grade` ");
		// 班级	
		$class_lists = $this->db->fetchAll("SELECT `class_id`,`class_name` FROM `tbl_class` ");

	    if (empty($manage_data)){
            Helper_Client::alert("信息不存在");
        }

        if ($_POST){ 

            $grade_id     			= $this->post->grade_id;
            $class_id   			= $this->post->class_id;
			$manage_title   		= $this->post->manage_title;                    

            // 写入数据库
            $info = array(
				
			"grade_id"  			=> $grade_id,
            "class_id"       		=> $class_id,
			"manage_title"       	=> $manage_title,				
            "status"                => 1,
            "create_time"           => date("Y-m-d H:i:s"),
            "update_time"           => "0000-00-00 00:00:00",  

			 );

          $this->db->do_update("tbl_manage", $info, "manage_id='{$manage_id}'");

          // 跳转
          Helper_Client::location("修改成功", "index.php?a=quantification.list");

         }

            $this->tpl->assign("manage_data", $manage_data);
			$this->tpl->assign("class_lists", $class_lists);
			$this->tpl->assign("grade_lists", $grade_lists);
	        $this->tpl->display("quantification/update.tpl.php");
    } 
	/** 班级量化删除 **/
    public function actionDel()
    {
        $manage_id = $this->get->manage_id;   
        $this->db->query("DELETE  FROM `tbl_manage` WHERE manage_id='{$manage_id}'");
         // 跳转
        Helper_Client::location("删除成功", "index.php?a=quantification.list");
    }
	
}

?>